package fr.labri.rest;


import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.core.util.MultivaluedMapImpl;
import com.sun.jersey.multipart.FormDataBodyPart;
import com.sun.jersey.multipart.FormDataMultiPart;
import com.sun.jersey.multipart.file.FileDataBodyPart;
import com.sun.jersey.multipart.impl.MultiPartWriter;
import com.sun.jersey.oauth.client.OAuthClientFilter;
import com.sun.jersey.oauth.signature.OAuthParameters;
import com.sun.jersey.oauth.signature.OAuthSecrets;

@Path("/files")
public class Files {

	@GET
	@Path("/infos")
	@Produces(MediaType.TEXT_HTML)
	public String infos() {
		
		Client client = Client.create(new DefaultClientConfig());
		WebResource resource = client
				.resource("https://api.dropbox.com/1/account/info").queryParam("locale", "fr");
		
		OAuthParameters params = new OAuthParameters();
		
		params.setSignatureMethod("HMAC-SHA1");
		params.setConsumerKey(Authentification.DROPBOX_CONSUMER_KEY);
		params.setToken(Authentification.DROPBOX_ACCESS_TOKEN);
		
		OAuthSecrets secrets = new OAuthSecrets();
			secrets.consumerSecret(Authentification.DROPBOX_CONSUMER_SECRET)
			.setTokenSecret(Authentification.secret);
		
		OAuthClientFilter filter = new OAuthClientFilter(client.getProviders(), params, secrets);
		resource.addFilter(filter);
		
		return resource.get(String.class);

	}
	
	
	
	@GET
	@Path("/listfiles")
	@Produces(MediaType.TEXT_HTML)
	public String listFiles(@QueryParam("dir") String _dir) {

		String strdir="";
		if (_dir != null && _dir !="" && _dir.length()>=1){
			try {
				strdir=URLEncoder.encode(_dir,"UTF-8")+"";
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		Client client = Client.create(new DefaultClientConfig());		
		WebResource resource = client
				.resource("https://api.dropbox.com/1/metadata/dropbox/"+strdir);

		
		OAuthParameters params = new OAuthParameters();
		
		params.setSignatureMethod("HMAC-SHA1");
		params.setConsumerKey(Authentification.DROPBOX_CONSUMER_KEY);
		params.setToken(Authentification.DROPBOX_ACCESS_TOKEN);
		
		OAuthSecrets secrets = new OAuthSecrets();
			secrets.consumerSecret(Authentification.DROPBOX_CONSUMER_SECRET)
			.setTokenSecret(Authentification.secret);
		
		OAuthClientFilter filter = new OAuthClientFilter(client.getProviders(), params, secrets);
		resource.addFilter(filter);
		
		return resource.get(String.class);

	}
	
	@GET
	@Path("/putfile")
	@Consumes(MediaType.MULTIPART_FORM_DATA)
	@Produces(MediaType.TEXT_HTML)
	public String putFile() 
	{
		
		ClientConfig cc = new DefaultClientConfig();
		cc.getClasses().add(MultiPartWriter.class);
		Client client = Client.create(cc);
		//Client client = Client.create(new DefaultClientConfig());
		
		
		WebResource resource = client
				.resource("https://api-content.dropbox.com/1/files_put/img.jpg");
		//https://api-content.dropbox.com/1/files_put/<root>/<path>?param=val
				//.queryParam("root", "dropbox")
				//.queryParam("path", "");
		
		File your_file = new File("img.jpg");
		
		MultivaluedMap<String, String> parametres = new MultivaluedMapImpl();
		parametres.add("title", "description"); 
		ClientResponse response = resource.queryParams(parametres).get(ClientResponse.class);
		
		OAuthParameters params = new OAuthParameters();
		
		params.setSignatureMethod("HMAC-SHA1");
		params.setConsumerKey(Authentification.DROPBOX_CONSUMER_KEY);
		params.setToken(Authentification.DROPBOX_ACCESS_TOKEN);
		
		OAuthSecrets secrets = new OAuthSecrets();
			secrets.consumerSecret(Authentification.DROPBOX_CONSUMER_SECRET)
			.setTokenSecret(Authentification.secret);
		
		OAuthClientFilter filter = new OAuthClientFilter(client.getProviders(), params, secrets);
		resource.addFilter(filter);
		

		File photo = new File("img.jpg");
		
		FormDataMultiPart form = new FormDataMultiPart();
		//FormDataBodyPart formBody = new FileDataBodyPart("photo", photo,MediaType.valueOf("image/jpeg"));
		FormDataBodyPart formBody = new FileDataBodyPart("photo", photo);//,MediaType.valueOf("image/jpeg"));
		form.field("title", "un super titre");
		form.bodyPart(formBody);
		
		MultivaluedMapImpl requestParams = new MultivaluedMapImpl();
	    requestParams.add("title", "test");

	    try {
	    	  response = resource
	    			 .queryParam("title", "un super titre")
	    		   //  .type(MediaType.MULTIPART_FORM_DATA).accept(MediaType.TEXT_PLAIN)
	    			 .type(MediaType.MULTIPART_FORM_DATA).accept(MediaType.TEXT_PLAIN)
	    			 .post(ClientResponse.class, form);

	    	String responseBody = response.getEntity(String.class);
	    	System.out.println(responseBody);
	    } catch (Exception e) {
	    	System.out.println(e.getMessage());
	    }
	    return resource.get(String.class);
	}
	
	@GET
	@Path("/getfile")
	@Produces(MediaType.MULTIPART_FORM_DATA)
	public String getFile() 
	{
		
		Client client = Client.create(new DefaultClientConfig());		
		WebResource resource = client.resource("https://api-content.dropbox.com/1/files/dropbox/img.jpg")
				.queryParam("rev", "");

		
		OAuthParameters params = new OAuthParameters();
		
		params.setSignatureMethod("HMAC-SHA1");
		params.setConsumerKey(Authentification.DROPBOX_CONSUMER_KEY);
		params.setToken(Authentification.DROPBOX_ACCESS_TOKEN);
		
		OAuthSecrets secrets = new OAuthSecrets();
			secrets.consumerSecret(Authentification.DROPBOX_CONSUMER_SECRET)
			.setTokenSecret(Authentification.secret);
		
		OAuthClientFilter filter = new OAuthClientFilter(client.getProviders(), params, secrets);
		resource.addFilter(filter);
		
		return "";//resource.get(String.class);
		
	}
	
	

}
